import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'login',
      component: () => import('../views/LoginView.vue')
    },
    {
      path: '/admin',
      meta:{requireAuth:true},
      name: 'admin',
      component: () => import('../views/admin/AdminView.vue'),
      children:[
        {path:"system/staff",
        component:()=>import('../views/admin/system/StaffView.vue')
        }
      ]
    },
    {
      path: '/about',
      name: 'about',
      // route level code-splitting
      // this generates a separate chunk (About.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import('../views/AboutView.vue')
    }
  ]
})

// beforeEach每个路由进入前执行的回调函数
// to要进入的路由,from从哪个路由跳转 next下一步操作
router.beforeEach((to,from,next)=>{
  // 如果元信息需要权限判断是否有token
  if(to.meta.requireAuth){
    if(sessionStorage.getItem("token")){
      next(); //放行
    }else{
      next('/'); //跳转到首页
    }
  }else{
    next();
  }
  
})
export default router
